package com.java.comprehensive.arithmetic.leecode.tags.array;

import cn.hutool.core.util.ArrayUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Dgg
 * @Date: 2022/1/22 0:25
 */
public class LeetCode_27 {

	public static int removeElement(int[] nums, int val) {
		int flag = 0;
		Map<Integer, Integer> map = new HashMap<>(16);
		for (int i = 0; i < nums.length; i++) {
			if (nums[i] != val) {
				map.put(flag, nums[i]);
				flag = flag + 1;
			}
		}
		map.forEach((k, v) -> {
			nums[k] = v;
		});

		return flag;
	}


	public static int removeElementTwo(int[] nums, int val) {
		int flag = 0;

		int l = 0;
		int r = nums.length - 1;
		while (l <= r) {
			if (nums[r] == val) {
				r--;
			} else {
				while (l <= r && nums[l] != val) {
					l++;
					flag += 1;
				}
				if (l < r) {
					nums[l] = nums[r];
					l++;
					r--;
					flag += 1;
				}
			}
		}
		return flag;
	}

	public static void main(String[] args) {
		int[] nums = {0, 1, 2, 2, 3, 0, 4, 2};
		int val = 2;
		int i = removeElementTwo(nums, val);
		System.out.println("nums" + ArrayUtil.toString(nums));
		System.out.println(i);
	}
}
